/*
 * Date 	: 2015.03.10 19:23:46
 * Author	: Newpub
 */
package com.gdface.admin.gdface_attend;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.pefis.natives.Ast2600JNI;
import com.pefis.natives.PfsBioInfo;

import java.nio.ByteBuffer;

public class VerifyActivity extends Activity implements Runnable{
	
	ImageView mVerifyFaceView;
	FaceDetectionView mFaceDetectionView;
	TextView mVerifyIDTxt;
	TextView mResultTxt;
	RatingBar mRatingBar; 
	ImageView mPhoto; 
	CheckBox mSwitchCamera;
	TextView mCandInfoText;
	private boolean mColorCamera;
	private int mImageWidth;
	private int mImageHeight;  
	private Thread mface_pro;
	public boolean m_bExitLoop = false; 
	private byte[] mColorImageFrame;
	private byte[] mProcessImageFrame;
	public byte[] m_UpdateTemplate; 
	public byte[] m_Template; 
	private byte[] mPhotoImage;
	private long[] mCandInfo;
	private int mContextID;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_verify);
		mVerifyFaceView = (ImageView)this.findViewById(R.id.EnrollFaceView);
		mFaceDetectionView = (FaceDetectionView)this.findViewById(R.id.FaceDetectionView);
		mVerifyIDTxt = (TextView)this.findViewById(R.id.identified_id);
		mResultTxt = (TextView)this.findViewById(R.id.result_txt);
		mPhoto = (ImageView)this.findViewById(R.id.photo);
		mSwitchCamera = (CheckBox)this.findViewById(R.id.switch_camera);
		mCandInfoText = (TextView)this.findViewById(R.id.cand_info);
		
		mImageWidth = PfsBioInfo.getInstance().face_img_w[0];
		mImageHeight = PfsBioInfo.getInstance().face_img_h[0];
		mColorImageFrame = new byte[mImageWidth*mImageHeight];
		mProcessImageFrame = new byte[mImageWidth*mImageHeight];
		mPhotoImage = new byte[mImageWidth*mImageHeight*4];
		m_UpdateTemplate =new byte[PfsBioInfo.getInstance().enroll_template_size[0]];
		m_Template = new byte[PfsBioInfo.getInstance().enroll_template_size[0]];
		mCandInfo = new long[PfsBioInfo.PFSBIO_CAND_STRUCT];
		mface_pro = new Thread(this);
		m_bExitLoop = true;
		mface_pro.start();
		mSwitchCamera.setOnCheckedChangeListener(mCheckedChangeListener);
		mColorCamera = true;
		mContextID = PfsBioInfo.getInstance().mContextID;
	}
	
	private OnCheckedChangeListener  mCheckedChangeListener = new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

			mColorCamera = !mColorCamera;
			// Log.e("","value = "+mCheckedDay);
		}
	};
	
	Handler mImageDraw= new Handler(){
		@Override
		public void handleMessage(Message msg){
			mVerifyFaceView.setImageBitmap((Bitmap)msg.obj);
		}
	};
	
	@Override
	public void onBackPressed(){
		finish();
		m_bExitLoop = false;
		PfsBioInfo.getInstance().killMediaPlayer();
		
	}
	@Override 
    public void onDestroy() {
        // Don't forget to shutdown!
    	PfsBioInfo.getInstance().killMediaPlayer();
    	
        super.onDestroy();
    }
	
	public void DrawFaceImage(byte[] rawData,Boolean IsSave) {
       
		if(mColorCamera)
			PfsBioInfo.getInstance().CreateColorBitmapFromBayer(rawData);
		else
			PfsBioInfo.getInstance().CreateGrayBitmapFromBayer(rawData);
			
		mImageDraw.obtainMessage(0,  PfsBioInfo.getInstance().m_bmp).sendToTarget();
    }
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		int vnRet;
		int[] update_tmpl_flag = new int[1];
	
		Ast2600JNI.getInstance().pisFcInfraredLedControl(mContextID, PfsBioInfo.PFSBIO_IR_LED_HIGH_ON);
		
		PfsBioInfo.getInstance().ReadByteFromFile(PfsBioInfo.TEMPLATE_DB_PATH+ PfsBioInfo.getInstance().mVerifyID, m_Template, 0);
		
		mVerifyIDTxt.setText(PfsBioInfo.getInstance().mVerifyID+"");
		short[] faceRect = new short[8];
		while(m_bExitLoop){ 
			if(mColorCamera)
			{
				Ast2600JNI.getInstance().pisFcColorImageCapture(PfsBioInfo.getInstance().mContextID,mColorImageFrame );
				Ast2600JNI.getInstance().pisFcGrayImageCapture(PfsBioInfo.getInstance().mContextID,mProcessImageFrame );
				DrawFaceImage(mColorImageFrame,true);
			}
			else
			{
				Ast2600JNI.getInstance().pisFcGrayImageCapture(PfsBioInfo.getInstance().mContextID,mProcessImageFrame );
				DrawFaceImage(mProcessImageFrame,true);
			}
			
			
			vnRet = Ast2600JNI.getInstance().pisFcCheck(mContextID, mProcessImageFrame, mImageWidth, mImageHeight, faceRect);
			
			if (vnRet != PfsBioInfo.PFSBIO_OK){
				Ast2600JNI.getInstance().pisLedControl(mContextID, PfsBioInfo.PFSBIO_OKNOLED, PfsBioInfo.PFSBIO_LED_OFF);
				
				mShowResult.sendEmptyMessage(0);  
				mFaceDetectionView.setRect(-10 , -10 , -10, -10);
				mFaceDetectionView.postInvalidate();
				continue;
			}
			
			mFaceDetectionView.setRect(faceRect[0] , faceRect[2] , faceRect[1] - faceRect[0], faceRect[3] - faceRect[2]);
			mFaceDetectionView.postInvalidate();
			
			vnRet = Ast2600JNI.getInstance().pisFcVerify(mContextID, mProcessImageFrame, mImageWidth, mImageHeight,  m_Template, mCandInfo, m_UpdateTemplate,update_tmpl_flag);

			if (vnRet == PfsBioInfo.PFSBIO_OK) {
				//PlaySound("MatOK.wav", NULL, SOUNDFLAG);
				Ast2600JNI.getInstance().pisLedControl(mContextID, PfsBioInfo.PFSBIO_OKLED, PfsBioInfo.PFSBIO_LED_ON);
				
				mShowResult.sendEmptyMessage(PfsBioInfo.getInstance().mVerifyID);
				
			    if (update_tmpl_flag[0] == 1) {
			    	PfsBioInfo.getInstance().WriteByteToFile(m_UpdateTemplate, PfsBioInfo.TEMPLATE_DB_PATH + PfsBioInfo.getInstance().mVerifyID);
			    }
			    
				
						
			}
			else{
				mShowResult.sendEmptyMessage(0);
				Ast2600JNI.getInstance().pisLedControl(mContextID, PfsBioInfo.PFSBIO_NOLED, PfsBioInfo.PFSBIO_LED_ON);
			}
			
		}
		Ast2600JNI.getInstance().pisFcInfraredLedControl(mContextID, PfsBioInfo.PFSBIO_IR_LED_LOW_ON);
		Ast2600JNI.getInstance().pisLedControl(mContextID, PfsBioInfo.PFSBIO_OKNOLED, PfsBioInfo.PFSBIO_LED_OFF);
		
	}
	
Handler mShowResult = new Handler(){
		
		@Override
		public void handleMessage(Message msg){
			String msg_str;
			String vCandMessage;
			if(msg.what != 0){   
				msg_str = ""+msg.what;
				mResultTxt.setText("Matched");
								int fileSize = 0;
				try {
					PfsBioInfo.getInstance().playAudio(PfsBioInfo.AUDIO_DING);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
				fileSize = PfsBioInfo.getInstance().ReadByteFromFile(PfsBioInfo.PHOTO_DB_PATH+ PfsBioInfo.getInstance().mVerifyID+".pv",mPhotoImage, 0);
				if(fileSize == -1 )
					return;
				Bitmap p = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
				p.copyPixelsFromBuffer(ByteBuffer.wrap(mPhotoImage));
				mPhoto.setImageBitmap(p);
				
				
				
			}
			else{  
				mResultTxt.setText("No Matched");  
				mPhoto.setImageBitmap(null);
				mPhoto.setBackgroundResource(R.drawable.noavatar_middle);
			}
			vCandMessage = "";
			for(int i = 0;i<mCandInfo[0];i++){
				vCandMessage += "  " + PfsBioInfo.getInstance().mVerifyID +" ("+mCandInfo[i + 5]+")\n";
			}
			mCandInfoText.setText(vCandMessage);					 
		} 
	};
}
 